package cn.com.xy.sms.sdk.ui.popu.widget;

import android.annotation.SuppressLint;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;

import java.util.LinkedList;
import java.util.List;

import cn.com.xy.sms.sdk.constant.Constant;
import cn.com.xy.sms.sdk.smsmessage.BusinessSmsMessage;
import cn.com.xy.sms.sdk.ui.R;
import cn.com.xy.sms.sdk.ui.popu.util.ContentUtil;
import cn.com.xy.sms.sdk.ui.popu.util.ViewUtil;
import cn.com.xy.sms.util.ParseManager;

public class DuoquProgressViewShowMoreInfo {

    public static final String KEY_EXPANDED = "expanded_cmcc";
    private static final int TYPE_EXPAND = 0;
    private static final int TYPE_COLLAPSE = 1;
    private ViewGroup mContainer = null;
    private List<DuoquProgressVerHolder> mMoreInfoViewHolder = new LinkedList<DuoquProgressVerHolder>();
    private View mExpandCollapseMoreInfoView = null;
    private ImageView mExpandMoreView = null;
    private TextView mExpandMoreTvView = null;
    private TextView mToOrgMessageWarn = null;
    private int mDefaultShowRow = 0;
    private int mMaxShowInfo = 0;

    public DuoquProgressViewShowMoreInfo(ViewGroup container) {
        mContainer = container;
    }

    /**
     * 添加需要隐藏/显示的更多信息ViewHolder
     * 
     * @param viewHolder
     */
    public void addMoreInfoViewHolder(DuoquProgressVerHolder viewHolder) {
        mMoreInfoViewHolder.add(viewHolder);
    }

    /**
     * 清空需要隐藏/显示的更多信息ViewHolder
     */
    public void clearMoreInfoViewHolderList() {
        mMoreInfoViewHolder.clear();
    }

    /**
     * 隐藏展开/收起控件
     * 
     */
    public void hiddenExpandCollapseMoreInfoView() {
        ContentUtil.setViewVisibility(mExpandCollapseMoreInfoView, View.GONE);
        ContentUtil.setViewVisibility(mToOrgMessageWarn, View.GONE);
    }

    /**
     * 显示展开/收起控件
     * 
     * @param message
     * @param expanded
     * @param lastItemId
     */
    public void showExpandCollapseMoreInfoView(final BusinessSmsMessage message, boolean expanded, int lastItemId) {
        /* SMARTISAN-492 zhaoxiachao 20170324 start */
        if (getExpanded(message)) {
            hiddenExpandCollapseMoreInfoView();
            return;
        }
        /* SMARTISAN-492 zhaoxiachao 20170324 end */
        if (mExpandCollapseMoreInfoView == null) {
            mExpandCollapseMoreInfoView = newExpandCollapseMoreInfoView();
            mExpandCollapseMoreInfoView.setPadding(0, ViewUtil.dp2px(Constant.getContext(), 10), 0, 0);
            LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            mContainer.addView(mExpandCollapseMoreInfoView, params);
        }

        setStyle(lastItemId);
        mExpandCollapseMoreInfoView.setTag(R.id.tag_business_sms_message, message);
        mExpandMoreView.setImageDrawable(getExpandCollapseMoreInfoDrawable(message, expanded ? TYPE_COLLAPSE : TYPE_EXPAND));
        ContentUtil.setViewVisibility(mExpandCollapseMoreInfoView, View.VISIBLE);
    }

    /**
     * 设置展开收起控件样式
     * 
     * @param lastItemId
     *            最后一个item的viewId，展开收起控件将below该viewId
     */
    private void setStyle(int lastItemId) {
        LayoutParams layoutParams = (LayoutParams) mExpandCollapseMoreInfoView
                .getLayoutParams();
        layoutParams.addRule(RelativeLayout.BELOW, lastItemId);
        layoutParams.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);
        layoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
    }

    /**
     * 创建展开收起控件
     * 
     * @return
     */
    @SuppressLint("NewApi")
    private View newExpandCollapseMoreInfoView() {
        final View expandCollapseMoreInfoView = LayoutInflater.from(mContainer.getContext()).inflate(R.layout.duoqu_progressbar_more, null);
        mExpandMoreView = (ImageView)expandCollapseMoreInfoView.findViewById(R.id.duoqu_two_items_table_expand_icon);
        mExpandMoreTvView = (TextView)expandCollapseMoreInfoView.findViewById(R.id.duoqu_two_items_table_expand);
        expandCollapseMoreInfoView.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                final BusinessSmsMessage businessSmsMessage = (BusinessSmsMessage) v
                        .getTag(R.id.tag_business_sms_message);
                if (businessSmsMessage == null) {
                    return;
                }
                int visibility = getExpanded(businessSmsMessage) ? View.GONE : View.VISIBLE;
                boolean isShow = visibility == View.VISIBLE;
                try {
                    businessSmsMessage.bubbleJsonObj.put(KEY_EXPANDED, (visibility == View.VISIBLE));
                    ParseManager.updateMatchCacheManager(businessSmsMessage);
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
                for (DuoquProgressVerHolder viewHolder : mMoreInfoViewHolder) {
                    viewHolder.setVisibility(visibility);
                }
                if (expandCollapseMoreInfoView != null) {
                    mExpandMoreView.setImageDrawable(getExpandCollapseMoreInfoDrawable(businessSmsMessage,
                            isShow ? TYPE_COLLAPSE : TYPE_EXPAND));
                }
                /* SMARTISAN-492 zhaoxiachao 20170324 start */
                hiddenExpandCollapseMoreInfoView();
                /* SMARTISAN-492 zhaoxiachao 20170324 end */
            }
        });
        return expandCollapseMoreInfoView;
    }

    @SuppressWarnings("deprecation")
    private Drawable getExpandCollapseMoreInfoDrawable(BusinessSmsMessage message, int type) {
        return mContainer.getContext().getResources().getDrawable(type == TYPE_EXPAND
                ? R.drawable.duoqu_expand : R.drawable.duoqu_collapse);
    }


    /**
     * 获取展开状态
     * 
     * @param message
     * @return true:展开 false:收起
     */
    public boolean getExpanded(BusinessSmsMessage message) {
        return message.bubbleJsonObj != null && message.bubbleJsonObj.optBoolean(KEY_EXPANDED);
    }

    /**
     * 设置默认显示数据行数
     * 
     * @param defaultShowRow
     */
    public void setDefaultShowRow(int defaultShowRow) {
        mDefaultShowRow = defaultShowRow;
    }

    /**
     * 获取默认显示数据行数
     * 
     * @return
     */
    public int getDefaultShowRow() {
        return mDefaultShowRow;
    }

    public int getmMaxShowInfo() {
        return mMaxShowInfo;
    }

    public void setmMaxShowInfo(int mMaxShowInfo) {
        this.mMaxShowInfo = mMaxShowInfo;
    }
}
